package lumis.service.portalmanagement.channel;

import lumis.doui.source.ISourceData;
import lumis.doui.source.Source;
import lumis.doui.source.TabularData;
import lumis.doui.source.postloadprocessor.IPostLoadProcessor;
import lumis.portal.PortalException;
import lumis.portal.PortalObjectNotFoundException;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.channel.ChannelConfig;
import lumis.portal.manager.ManagerFactory;
import lumis.portal.page.PageConfig;
import lumis.portal.propertybag.Property;
import lumis.portal.propertybag.PropertyBag;
import lumis.portal.url.IURLManager;
import lumis.util.ITransaction;

import java.util.List;

import org.w3c.dom.Node;

/**
 * Post load processor that loads the private property bag properties of the channel.
 *
 * @version $Revision: 11372 $ $Date: 2010-04-01 16:08:18 -0300 (Thu, 01 Apr 2010) $
 * @since 5.0.0
 */
public class ChannelPropertyBagPostLoadProcessor implements IPostLoadProcessor
{

	public void processSource(SessionConfig sessionConfig, Source source, Node parametersNode, ITransaction transaction) throws PortalException
	{
		TabularData tabularData = (TabularData) source.getData();
		for (ISourceData row : tabularData.getRows())
		{
			String id = row.get("channelId", String.class);
			ChannelConfig channel = ManagerFactory.getChannelManager().get(sessionConfig, id, transaction);
			PropertyBag propertyBag = ManagerFactory.getPropertyBagManager().get(channel.getPropertyBagId());

			// get the enable friendly url property
			try
			{
				List<String> values = propertyBag.getLocalProperty(IURLManager.FRIENDLY_URL_ENABLE_PROPERTY_NAME).getDefaultValues();
				if(values != null && values.size() > 0)
					row.put("enableFriendlyUrl", values.get(0));
			}
			catch(PortalObjectNotFoundException e)
			{
			}

			// get the friendly url pattern property
			try
			{
				List<String> values = propertyBag.getLocalProperty(IURLManager.FRIENDLY_URL_PATTERN_PROPERTY_NAME).getDefaultValues();
				if(values != null && values.size() > 0)
					row.put("friendlyUrlPropertyBagPattern", values.get(0));
			}
			catch(PortalObjectNotFoundException e)
			{
			}

			// get the friendly url pattern property
			try
			{
				List<String> values = propertyBag.getLocalProperty(IURLManager.WEBSITE_PROPERTY_NAME).getDefaultValues();
				if(values != null && values.size() > 0)
					row.put("websiteUrl", values.get(0));
			}
			catch(PortalObjectNotFoundException e)
			{
			}
			
			// get the inherited layout file value
			String parentChannelId = channel.getParentId();
			if (parentChannelId != null)
			{
				ChannelConfig parentChannelConfig = ManagerFactory.getChannelManager().get(sessionConfig, parentChannelId, transaction);
				String value = parentChannelConfig.getLayoutFilePath();
				if (value != null)
					row.put("inheritedLayoutFile", value);
			}
			
			// get the local layout file value
			try
			{
				Property localProperty = propertyBag.getLocalProperty(PageConfig.LAYOUT_FILE_PATH_PROPERTY_NAME);
				List<String> defaultValues = localProperty.getDefaultValues();
				if(defaultValues != null && !defaultValues.isEmpty())
				{
					String value = defaultValues.get(0);
					if(value != null)
						row.put("currentLayoutFile", value);
				}
			}
			catch (PortalObjectNotFoundException e)
			{
				// ignore if property is not present
			}
		}
	}
}
